import { cloneDeep } from "lodash";
import * as TYPE from "../action-type";
import { convertImgUrl } from "@/utils/MyFun";

const initialValue = {
  // mobileFlag: false, // 是否是移动端
  reloadFlag: true,

  searchFlag: false, // 搜索
  loginFlag: false, // 登录
  registerFlag: false, // 注册
  forgetFlag: false, // 忘记密码

  collapsed: false, // 侧边栏折叠 (移动端)

  // TODO: 将变量拆分开来
  blogInfo: {
    blog_config: {
      website_name: "阵、雨的个人博客",
      website_author: "阵、雨",
      website_intro: "往事随风而去",
    },
  },
};

export const appReducer = (state = initialValue, action) => {
  state = cloneDeep(state);
  switch (action.type) {
    case TYPE.SETCOLLAPSED:
      state.collapsed = action.collapsed;
      break;
    case TYPE.SETLOGINFLAG:
      state.loginFlag = action.loginFlag;
      break;
    case TYPE.SETREGISTERFLAG:
      state.registerFlag = action.registerFlag;
      break;
    case TYPE.SETFORGETFLAG:
      state.forgetFlag = action.forgetFlag;
      break;
    case TYPE.SETSEARCHFLAG:
      state.searchFlag = action.searchFlag;
      break;
    case TYPE.APP_GETBLOGINFO:
      state.blogInfo = { ...state.blogInfo, ...action.blogInfo };
      state.blogInfo.page_list = state.blogInfo.page_list.map((item) => {
        item.cover = convertImgUrl(item.cover);
        return item;
      });
      state.blogInfo.blog_config.website_avatar = convertImgUrl(
        state.blogInfo.blog_config.website_avatar
      );
      break;
    default:
  }
  return state;
};
